package com.bj.disc.widget

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.animation.LinearInterpolator
import androidx.appcompat.widget.AppCompatImageView
import com.ai3d.rfid.R
import com.ai3d.rfid.common.utils.HandlerUtils
import com.blankj.utilcode.util.ConvertUtils

class JumpingArrowView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {

    private var jumpAnimator: ObjectAnimator? = null
    private var jumpDistanceDP = 50f
    private var animationDuration = 500L
    private var repeatMode = ObjectAnimator.REVERSE
    private var rotationAngle = 0f
    private var isAutoPlay=true

    /**
     * float translationY = view.getY() - 50; // 相对视图上方50dp的位置
    float translationYTop = view.getY() - 50; // 上方位置
    float translationYBottom = view.getY() + 50; // 下方位置

    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", translationYTop, translationYBottom, translationYTop);
    animator.setDuration(1000); // 动画持续时间，可根据需要进行调整
    animator.setRepeatCount(ObjectAnimator.INFINITE); // 无限循环
    animator.setRepeatMode(ObjectAnimator.RESTART); // 循环模式

    animator.start(); // 启动动画

     */

    init {
        setupAttributes(attrs)
        initJumpAnimator()
        rotation = rotationAngle
    }

    private fun initJumpAnimator() {
        jumpAnimator = ObjectAnimator.ofFloat(this, "translationY", 0f, convertDpToPx(jumpDistanceDP))
        jumpAnimator?.duration = animationDuration
        jumpAnimator?.repeatCount = ObjectAnimator.INFINITE
        jumpAnimator?.interpolator= LinearInterpolator()
        jumpAnimator?.repeatMode = repeatMode
    }

    private fun setupAttributes(attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JumpingArrowView)
        jumpDistanceDP = typedArray.getDimension(
            R.styleable.JumpingArrowView_jumpDistance,
            convertDpToPx(jumpDistanceDP)
        )
        animationDuration = typedArray.getInt(
            R.styleable.JumpingArrowView_animationDuration,
            animationDuration.toInt()
        ).toLong()
        repeatMode = typedArray.getInt(
            R.styleable.JumpingArrowView_repeatMode,
            repeatMode
        )
        isAutoPlay=typedArray.getBoolean(
            R.styleable.JumpingArrowView_repeatMode,
            true
        )
        rotationAngle = typedArray.getFloat(R.styleable.JumpingArrowView_rotationAngle, rotationAngle)

        typedArray.recycle()
    }

    fun setJumpDistance(distanceDP: Float) {
        jumpDistanceDP = distanceDP
        jumpAnimator?.setFloatValues(0f, convertDpToPx(jumpDistanceDP))
    }

    fun setAnimationDuration(duration: Long) {
        animationDuration = duration
        jumpAnimator?.duration = animationDuration
    }

    fun setRepeatMode(mode: Int) {
        repeatMode = mode
        jumpAnimator?.repeatMode = repeatMode
    }

    fun setRotationAngle(angle: Float) {
        rotationAngle = angle
        rotation = rotationAngle
    }

    override fun setRotation(rotation: Float) {
        super.setRotation(rotation)
        invalidate() // 刷新视图，使旋转角度生效
    }

    fun startJumpingAnimation() {
        jumpAnimator?.start()
    }

    fun stopJumpingAnimation() {
        jumpAnimator?.cancel()
    }

    private fun convertDpToPx(dp: Float): Float {
       return ConvertUtils.dp2px(dp).toFloat()
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus&&isAutoPlay) {
                startJumpingAnimation()
        } else {
            stopJumpingAnimation()
        }
    }
}


